package one.stand.controller;

import com.github.pagehelper.PageHelper;
import com.jmp.api.crm.dto.CompanySelectedConfigRequest;
import com.jmp.api.crm.dto.CompanySelectedLabelResponse;
import com.jmp.api.crm.dto.CompanySelectedListResponse;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.mapper.CompanyCustomerLabelApiCrmMapper;
import one.stand.mapper.CompanyMapperExtPublic;
import one.stand.model.Company;
import one.stand.model.CompanyCustomerLable;
import one.stand.model.ResultModel;
import one.stand.service.CompanySelectService;
import one.stand.service.user.UserAdminService;
import one.stand.util.DateUtil;
import one.stand.util.RequestContextHolder;
import one.stand.util.SqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("crmPc/company/selected")
@Api(description = "优选平台-我的代理商")
public class CompanySelectedController {
    @Autowired
    private CompanyMapperExtPublic companyMapperExtPublic;
    @Autowired
    private UserAdminService userAdminService;
    @Autowired
    private CompanySelectService companySelectService;
    @Autowired
    private CompanyCustomerLabelApiCrmMapper companyCustomerLabelApiCrmMapper;

    @ApiOperation(value = "配置")
    @PostMapping("/config")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<CompanySelectedListResponse> config(@RequestAttribute CompanySelectedConfigRequest request) {
        final Integer userCompanyId = RequestContextHolder.getRequestContext().getUser().getCompanyId();

        String nameLike = SqlUtil.like(request.getNameLike());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<Company> companyList = companyMapperExtPublic.selectAgent(nameLike, request.getLabelId(), userCompanyId);
        PageResponse2<CompanySelectedListResponse.CompanySelectedModel> companySelectedListPage = ConvertUtils.page2(companyList, company -> CompanySelectedListResponse.CompanySelectedModel.builder()
                .companyName(company.getCompanyName())
                .phone(company.getPhone())
                .userName(userAdminService.get(company.getUserId()).getName())
                .labelList(companyCustomerLabelApiCrmMapper.selectLabelNameByCompanyId(company.getCompanyId()))
                .agentTime(DateUtil.format(DateUtil.formatter, companySelectService.getByCompanyId(company.getCompanyId()).getCreateTime()))
                .build());

        CompanySelectedListResponse response = CompanySelectedListResponse.builder()
                .companySelectedList(companySelectedListPage)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "标签")
    @PostMapping("/label")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<List<CompanySelectedLabelResponse>> label() {
        final Integer userCompanyId = RequestContextHolder.getRequestContext().getUser().getCompanyId();

        List<CompanyCustomerLable> list = companyCustomerLabelApiCrmMapper.selectCompanyCustomerLabel(userCompanyId);
        List<CompanySelectedLabelResponse> responseList = ConvertUtils.list(list, companyCustomerLable -> CompanySelectedLabelResponse.builder()
                .labelId(companyCustomerLable.getLableId())
                .labelName(companyCustomerLable.getLableName())
                .build());

        return ResultModel.success(responseList);
    }
}
